<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="isShowDialog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimpleAPI } from '@/api/employees'
import { addDepartmentAPI, getDepartDetailAPI, editDepartDetailAPI, getDepartmentAPI } from '@/api/departments'
import { Message } from 'element-ui'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      required: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    var checkRepeatCode = async(rule, value, callback) => {
      const { depts } = await getDepartmentAPI()
      const res = depts.find(item => item.code === value)
      if (res && res.id !== this.formData.id) {
        callback(new Error('输入的code值重复了，请重新输入'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      userList: []
    }
  },
  async created() {
    const res = await getEmployeeSimpleAPI()
    // console.log(res)
    this.userList = res
  },
  methods: {
    async btnOk() {
      await this.$refs.form.validate() // 非空校验
      if (this.formData.pid) {
        await editDepartDetailAPI(this.formData)
      } else {
        this.formData.pid = this.nodeData.id
        await addDepartmentAPI(this.formData)
      }

      Message.success('操作成功')
      this.btnCancel()
    },
    btnCancel() {
      this.$refs.form.resetFields()
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$emit('closeDialog') // 通过父组件进行关闭
      this.$emit('reload') // 通过父组件进行页面刷新
    },
    async getDepart(id) {
      const res = await getDepartDetailAPI(id)
      this.formData = res
    }
  }
}
</script>

<style>

</style>
